package com.example.teesystem.controller;

import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.entity.vo.library.IssueLibraryInsertVo;
import com.example.teesystem.entity.vo.library.LibrarySelectVo;
import com.example.teesystem.entity.vo.library.TipLibraryInsertVo;
import com.example.teesystem.service.LibraryService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author pxr
 * @data 2022/4/26 9:14
 */
@RestController
@RequestMapping(value = "/tee/library",produces = "application/json; charset=utf-8")
@RequiredArgsConstructor
@Validated
public class LibraryController {

    private final LibraryService libraryService;

    /**
     * 添加个人笔记
     * @param tipLibraryInsertVo
     * @return
     */
    @PostMapping(value = "/addTip")
    public Response addTip(@RequestBody @Valid TipLibraryInsertVo tipLibraryInsertVo){
        return libraryService.uploadTipLibrary(tipLibraryInsertVo);
    }

    /**
     * 收藏题目
     * @param issueLibraryInsertVo
     * @return
     */
    @PostMapping(value = "/addIssueTip")
    public Response addIssueTip(@RequestBody @Valid IssueLibraryInsertVo issueLibraryInsertVo){
        return libraryService.uploadIssueLibrary(issueLibraryInsertVo);
    }

    /**
     * 获取我的收藏
     * @param librarySelectVo
     * @return
     */
    @PostMapping(value = "/getMyLibrary")
    public Response getMyLibrary(@RequestBody @Valid LibrarySelectVo librarySelectVo){
        return libraryService.get(librarySelectVo);
    }

    /**
     * 删除个人收藏
     * @param map
     * @return
     */
    @PostMapping(value = "/delete")
    public Response deleteMyLibrary(@RequestBody Map<String,String> map){
        int id;
        try{
            id = Integer.parseInt(map.get("id"));
            System.out.println(id);
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_ERROR);
        }
        return libraryService.delete(id);
    }

}
